package com.item.controller;


import com.item.domain.EduMenuList;
import com.item.po.UserRoleRPo;
import com.item.service.EduMenuListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class EduMenuListController {

    @Autowired
    EduMenuListService eduMenuListService;

    @PostMapping(value = "/EduMenuList/{limit}/{page}")
    public List<EduMenuList> EduMenuList(
            @PathVariable(value = "limit", required = false) int limit,
            @PathVariable(value = "page", required = false) int page,
            @RequestBody(required = false) UserRoleRPo eduMenuList) {
        List<EduMenuList> eduMenuLists = eduMenuListService.findAll(limit, page, eduMenuList);

        for (EduMenuList pay : eduMenuLists) {
            System.out.println(pay.getTitle());
        }
//        for (EduMenuList menuList : eduMenuLists) {
//            if(menuList.getChildren() == null || menuList.getChildren().size() == 0){
//                menuList.setChildren(new ArrayList<>());
//            }
//
//        }
        return eduMenuLists;
    }

    @PostMapping(value = "/eduMenuList/add")
    public Integer eduMenuListAdd(@RequestBody EduMenuList eduMenuList) {

        int i = eduMenuListService.add(eduMenuList);
        return i;
    }

    @PutMapping(value = "/eduMenuList/update")
    public Integer eduMenuListUpdate(@RequestBody EduMenuList eduMenuList) {

        int i = eduMenuListService.update(eduMenuList);
        return i;
    }

    @DeleteMapping(value = "/eduMenuList/{id}")
    public Integer eduMenuListDelete(@PathVariable(value = "id") int id) {

        int i = eduMenuListService.delete(id);
        return i;
    }

    @GetMapping(value = "/edu/menu/list/all")
    public List<EduMenuList> eduMenuListAll() {
        return eduMenuListService.eduMenuListAll();
    }

    @PostMapping(value = "/edu/menu/list/all/role")
    public List<EduMenuList> eduMenuListAllRole(@RequestBody(required = false) UserRoleRPo eduMenuList) {
        List<Integer> list = new ArrayList<>();
        if (eduMenuList.getMenuId() != null) {
            String s1 = "";
            if (eduMenuList.getMenuId().contains("all")) {
                s1 = eduMenuList.getMenuId().replaceAll(",all", "");
                for (String menuIds : s1.split(",")) {
                    list.add(Integer.valueOf(menuIds));
                }
            } else {
                for (String menuId : eduMenuList.getMenuId().split(",")) {
                    list.add(Integer.valueOf(menuId));
                }
            }
            eduMenuList.setIds(list);
        }
        List<EduMenuList> eduMenuLists = eduMenuListService.eduMenuListAllRole(eduMenuList);
        return eduMenuLists;
    }
}
